package com.shuoyue.ycgk.ui.news;

import android.Manifest;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.appcatch.FileUtil;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.entity.AppVersionInfo;
import com.shuoyue.ycgk.entity.News;
import com.shuoyue.ycgk.entity.NewsType;
import com.shuoyue.ycgk.entity.NoticeFile;
import com.shuoyue.ycgk.ui.common.download.DownloadContract;
import com.shuoyue.ycgk.ui.main.VersionContract;
import com.shuoyue.ycgk.ui.news.contract.NewsContract;
import com.shuoyue.ycgk.utils.AppUtil;
import com.shuoyue.ycgk.utils.MyRichTextHelper;
import com.shuoyue.ycgk.utils.NOpenFiles;
import com.shuoyue.ycgk.utils.ShareUtil;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;
import com.shuoyue.ycgk.views.ResizableImageView;
import com.zzhoujay.richtext.RichText;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import pub.devrel.easypermissions.EasyPermissions;

public class NewsInfoActivity extends BaseMvpActivity<NewsContract.Presenter>
        implements NewsContract.View, DownloadContract.View  {


    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.img)
    ResizableImageView img;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.tag)
    TextView tag;
    @BindView(R.id.content)
    TextView content;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.filename)
    RecyclerView filename;
    @BindView(R.id.back)
    ImageButton back;
    @BindView(R.id.scorllView)
    NestedScrollView scorllView;
    @BindView(R.id.top)
    ImageView top;
    int id;
    NoticeFileAdapter mAdapter;
    DownloadContract.Presenter downloadPresenter;
    @BindView(R.id.iv_right)
    ImageView ivRight;


    News newsInfo;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_news_info;
    }

    @Override
    protected void initView() {
        pageTitle.setText("详情");
        ivRight.setVisibility(View.VISIBLE);
        ivRight.setImageResource(R.mipmap.share);
        mAdapter = new NoticeFileAdapter(new ArrayList());
        filename.setAdapter(mAdapter);
        filename.setLayoutManager(new LinearLayoutManager(mContext));
        filename.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 4), false));
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            NoticeFile noticeFile = mAdapter.getItem(position);
            switch (view.getId()) {
                case R.id.tv_title:
                    String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
                    if (EasyPermissions.hasPermissions(mContext, permissions)) {
                        File dir = new File(FileUtil.getPath(mContext) + "/资讯/");
                        if (!dir.exists()) {
                            dir.mkdirs();
                        }

                        File file = new File(FileUtil.getPath(mContext) + "/资讯/", noticeFile.getAttachmentName());
                        if (file.exists()) {
                            NOpenFiles.openFile(NewsInfoActivity.this, file);
                        } else {
                            downloadPresenter.downloadFile(noticeFile.getAttachmentUrl(),
                                    new File(FileUtil.getPath(mContext) + "/资讯/", noticeFile.getAttachmentName()).getAbsolutePath());
                        }

                    } else {
                        EasyPermissions.requestPermissions(NewsInfoActivity.this,
                                "需要用到文件访问权限", 1, permissions);
                    }

                    break;
            }
        });
        registScrollToTop(scorllView);
    }


    @Override
    public void initData() {
        id = getIntent().getIntExtra("id", 0);
        mPresenter = new NewsContract.Presenter();
        mPresenter.attachView(this);
        downloadPresenter = new DownloadContract.Presenter();
        downloadPresenter.attachView(this);

        mPresenter.getNewsInfo(id);
    }


    @Override
    public void setNewsInfo(News newsInfo) {
        this.newsInfo = newsInfo;
        if (TextUtils.isEmpty(newsInfo.getCoverUrl())) {
            img.setVisibility(View.GONE);
        } else {
            Glide.with(mContext).load(newsInfo.getCoverUrl()).error(R.mipmap.img_loading_gray).into(img);
        }
        title.setText(newsInfo.getTitle());
        time.setText(newsInfo.getUpdateTime());
        tag.setVisibility(newsInfo.getBrowseNum() > 1000 ? View.VISIBLE : View.GONE);
        MyRichTextHelper.showRich(mContext, newsInfo.getContent(), content);

        if (newsInfo.getAttachmentList() != null && newsInfo.getAttachmentList().size() > 0) {
            mAdapter.resetData(newsInfo.getAttachmentList());
        }
    }

    @Override
    public void setNewsList(ListWithPage<News> newsList) {

    }

    @Override
    public void setNewsTypeList(List<NewsType> newsTypeList) {

    }

    @Override
    protected void onDestroy() {
        RichText.clear(this);
        super.onDestroy();

    }


    @OnClick({R.id.back, R.id.iv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.iv_right:
                if (newsInfo != null)
                    ShareUtil.shareUrl(this, ShareUtil.NEWS_URL + id,
                            newsInfo.getTitle(),
                            "",
                            null);
                break;
        }
    }


    @Override
    public void downloadSuc(File f) {
        NOpenFiles.openFile(this, f);
    }

    @Override
    public void progress(int progress) {

    }

    @Override
    public void downloadStart() {

    }

    @Override
    public void downloadError(String msg) {

    }

}
